/*****************************************************************************
 * hal_bk72_uart.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hal/source/hal_bk72/hal_bk72.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/

/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/

PLUM_PRIVATE
bk_uart_t hal_bk72_uart_idx(plum_u32 id)
{
    plum_u32 idx = PLUM_HAL_UART_IDX(id);

    bk_uart_t bk =
        ((idx == 1) ? BK_UART_1 : ((idx == 2) ? BK_UART_2 : BK_UART_MAX));

    return (bk);
}

PLUM_PRIVATE
uart_data_width_t hal_bk72_uart_databit(plum_hal_uart_databit_t bit)
{
    uart_data_width_t databit = BK_DATA_WIDTH_8BIT;

    switch (bit) {
        case PLUM_HAL_UART_DATABIT_5:
            databit = BK_DATA_WIDTH_5BIT;
            break;
        case PLUM_HAL_UART_DATABIT_6:
            databit = BK_DATA_WIDTH_6BIT;
            break;
        case PLUM_HAL_UART_DATABIT_7:
            databit = BK_DATA_WIDTH_7BIT;
            break;
        case PLUM_HAL_UART_DATABIT_8:
            databit = BK_DATA_WIDTH_8BIT;
            break;
        default:
            break;
    }
    return (databit);
}

PLUM_PRIVATE
uart_parity_t hal_bk72_uart_parity(plum_hal_uart_parity_t p)
{
    uart_parity_t parity = BK_PARITY_NO;

    switch (p) {
        case PLUM_HAL_UART_PARITY_NONE:
            parity = BK_PARITY_NO;
            break;
        case PLUM_HAL_UART_PARITY_ODD:
            parity = BK_PARITY_ODD;
            break;
        case PLUM_HAL_UART_PARITY_EVEN:
            parity = BK_PARITY_EVEN;
            break;
        default:
            break;
    }
    return (parity);
}

PLUM_PRIVATE
uart_stop_bits_t hal_bk72_uart_stopbit(plum_hal_uart_stopbit_t s)
{
    uart_stop_bits_t stopbit = BK_STOP_BITS_1;

    switch (s) {
        case PLUM_HAL_UART_STOPBIT_1:
            stopbit = BK_STOP_BITS_1;
            break;
        case PLUM_HAL_UART_STOPBIT_2:
            stopbit = BK_STOP_BITS_2;
            break;
        default:
            break;
    }
    return (stopbit);
}

PLUM_PRIVATE
uart_flow_control_t hal_bk72_uart_flowctrl(plum_hal_uart_flow_ctrl_t c)
{
    uart_flow_control_t flowctrl = FLOW_CTRL_DISABLED;

    switch (c) {
        case PLUM_HAL_UART_FLOW_CTRL_NONE:
            flowctrl = FLOW_CTRL_DISABLED;
            break;
        case PLUM_HAL_UART_FLOW_CTRL_CTS:
            flowctrl = FLOW_CTRL_CTS;
            break;
        case PLUM_HAL_UART_FLOW_CTRL_RTS:
            flowctrl = FLOW_CTRL_RTS;
            break;
        case PLUM_HAL_UART_FLOW_CTRL_RTS_CTS:
            flowctrl = FLOW_CTRL_RTS_CTS;
            break;
        default:
            break;
    }
    return (flowctrl);
}
/*****************************************************************************
 * Public Functions
 ****************************************************************************/

PLUM_PUBLIC
plum_s32 plum_hal_uart_init(plum_u32 id, plum_hal_uart_cof_t *config)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_UART(id) || !config) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

        bk_uart_t uart = hal_bk72_uart_idx(id);
        if (uart == BK_UART_MAX) {
            rc = PLUM_ECODE_EIO;
            break;
        }

        bk_uart_config_t cof;
        cof.baud_rate    = config->baudrate;
        cof.data_width   = hal_bk72_uart_databit(config->databit);
        cof.parity       = hal_bk72_uart_parity(config->parity);
        cof.stop_bits    = hal_bk72_uart_stopbit(config->stopbit);
        cof.flow_control = hal_bk72_uart_flowctrl(config->flowctrl);
        cof.flags        = 1;
        bk_uart_initialize(uart, &cof, );
    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_uart_send(plum_u32 id, const plum_void *data, plum_u16 cnt)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_UART(id) || !data) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

    } while (0);

    return (rc);
}

PLUM_PUBLIC
plum_s32 plum_hal_uart_recv(plum_u32 id, void *data, plum_u16 size,
                            plum_u32 timeout)
{
    plum_s32 rc = PLUM_ECODE_OK;

    do {
        if (!PLUM_HAL_ID_IS_UART(id) || !data) {
            rc = PLUM_ECODE_EPARA;
            break;
        }

    } while (0);

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
